package edu.guigu.networkconnection;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import edu.guigu.networkconnection.model.RepoResult;
import edu.guigu.networkconnection.repolist.IRepoView;
import edu.guigu.networkconnection.repolist.PeopPresenter;

public class MainActivity extends AppCompatActivity implements IRepoView {

    PeopPresenter presenter ;
    ProgressDialog progressDialog =null;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textview);

        presenter = new PeopPresenter(this);
        presenter.queryRemoteRepo();
    }

    @Override
    public void startLoading() {
        progressDialogBox();
        progressDialog.show();
    }

    @Override
    public void showListData(RepoResult body) {
        int dataSize = body.repoList.size();
        runOnUiThread(() -> {
            textView.setText("展示列表：" + dataSize);
        });
    }

    @Override
    public void endLoading() {
        if(progressDialog!=null){
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void showErro(Throwable t) {
        Toast.makeText(this, "获取失败!!", Toast.LENGTH_SHORT).show();
    }

    private void progressDialogBox() {
        // ProgressDialog.show(DialogboxActivity.this, "loading","资源加载中...", false, true);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("加载中");
        progressDialog.setMessage("资源加载中...请稍后");
        progressDialog.setCancelable(true);
        // STYLE_HORIZONTAL 水平
        // STYLE_SPINNER  圆形
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    }
}


//HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//// 1 构造OkHttp连接对象
//OkHttpClient client = new OkHttpClient.Builder()
//        .addInterceptor(interceptor)  // 添加日志拦截器
//        .build();
//// 2 构造请求
//Request request = new Request.Builder()
//        .get()
//        .url("https://api.github.com/users")
//        .build();
//// 3 创建Call
//Call call = client.newCall(request);
//// 4 call执行 (异步)
//        call.enqueue(new Callback() {
//    // 失败,注意是工作线程
//    @Override
//    public void onFailure(@NonNull Call call, @NonNull IOException e) {
//
//    }
//    // 成功响应,注意是工作线程
//    @Override
//    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//        if(response.code() == 200){
//            String jsonString = response.body().string();
//            // josn格式 -> 实例对象
//
//            Gson gson = new Gson();
//            // User user = gson.fromJson(jsonString, User.class);
//
//            ArrayList<User> userList = gson.fromJson(
//                    jsonString,
//                    new TypeToken<ArrayList<User>>(){}.getType()
//            );
//            Log.d("TAG", "onResponse: " + userList.size());
//        }
//    }
//});
